<?php

class Page_Be_Setup_VoucherGroups extends Page
{
    protected $tableObject;

    function __construct() {
        $this->idTableObject = get('id', 'int');
        $this->actionsTableObject = array(
            'addEditAction',
            'saveAction'
        );
        $this->classTableObject = 'Db_Setup_VoucherGroups';
        parent::__construct();
    }

    public function defaultAction() {
        if (perm('VOUCHER_GROUPS_EDIT')) {
            $this->addBreadcrumb(t(T_FORM_BUTTON, 'add'), '?action=addEdit');
        }
        $allVoucherGroups = Db_Setup_VoucherGroups::findAllArray();
        self::$TPL->assign('allVoucherGroups', $allVoucherGroups);
        self::$TPL->setBodyTemplate('setup/list_voucher_groups');
        self::$TPL->appendJsFile('admin/voucher_groups');
        return array(
            'type' => 'display'
        );
    }

    public function addEditAction() {
        perm('VOUCHER_GROUPS_EDIT', self::$PAGE['p_url']);
        $voucherType = Db_Setup_VoucherGroups::getEnumValues('vg_type');
        if (isset($this->idTableObject) && is_int($this->idTableObject)) {
            $this->addBreadcrumb(t(T_FORM_BUTTON, 'edit') . '&quot;', '?action=addEdit&id=' . $this->idTableObject, true);
            self::$TPL->assign('currentVoucherGroups', $this->tableObject->objectArray);
            self::$TPL->assign('voucherType', $voucherType);
        } else {
            $this->addBreadcrumb(t(T_FORM_BUTTON, 'add'), '?action=addEdit', true);
        }
        self::$TPL->setBodyTemplate('add_edit_voucher_groups');
        self::$TPL->appendJsFile('admin/voucher_groups');
        return array(
            'type' => 'display'
        );
    }

    public function saveAction() {
        perm('VOUCHER_GROUPS_EDIT', self::$PAGE['p_url']);
        $this->message = t(T_FORM_MSG, 'success');

        $voucherName = post('vg_name', 'string');
        $voucherType = post('vg_type', 'string');
        $voucherFixedAmount = post('vg_fixed_amount', 'string');
        $voucherPercentage = post('vg_percentage', 'string');
        $voucherMinOrderValue = post('vg_min_order_value', 'string');
        $voucherUnique = post('vg_unique', 'int');

        if (!empty($voucherName) && !empty($voucherType) && !empty($voucherMinOrderValue)  && (!empty($voucherFixedAmount) || !empty($voucherPercentage))) {
            if($this->idTableObject){
                $this->tableObject->id = $this->idTableObject;
            }
            $this->tableObject->vg_name = $voucherName;
            $this->tableObject->vg_type = $voucherType;
            $this->tableObject->vg_fixed_amount = $voucherFixedAmount;
            $this->tableObject->vg_unique = $voucherUnique;
            $this->tableObject->vg_min_order_value = $voucherMinOrderValue;
            $this->tableObject->vg_fixed_amount = $voucherFixedAmount;
            $this->tableObject->vg_percentage = $voucherPercentage;

            if (!$this->tableObject->save()) {
                $this->error = true;
                $this->message = t(T_FORM_ERROR, 'unknown_error_occurred');
            }
        } else {
            $this->error = true;
            $this->message = t(T_FORM_ERROR, 'required_fields');
        }

        return array(
            'type' => 'redirect',
            'query' => false
        );
    }

    public function deleteAction() {
        perm('VOUCHER_GROUPS_EDIT', self::$PAGE['p_url']);
        $this->message = t(T_FORM_MSG, 'success');
        if (isset($this->idTableObject) && is_int($this->idTableObject)) {
            if (!Db_Setup_VoucherGroups::abstractDelete($this->idTableObject)) {
                $this->error = true;
                $this->message = t(T_FORM_ERROR, 'unknown_error_occurred');
            }
        } else {
            $this->error = true;
            $this->message = t(T_FORM_ERROR, 'unknown_error_occurred');
        }
        return array(
            'type' => 'redirect',
            'query' => false
        );
    }

    public function ajaxLoadVoucherGroupsAction() {
        $allColumns = array('nr', 'vg_name', 'vg_type', 'vg_fixed_amount', 'vg_percentage', 'vg_min_order_value', 'vg_unique', 'action');
        $searchColumns = array('vg_name', 'vg_type', 'vg_fixed_amount', 'vg_percentage', 'vg_min_order_value');

        list($searchQuery,$orderBy, $limit) = Utils::getSqlQueriesForPagination($allColumns, $searchColumns, 'Db_Setup_VoucherGroups', 'getTotalVoucherGroups');
        $allVoucherGroups = Db_Setup_VoucherGroups::getAllVoucherGroupsPerPage($limit, $searchQuery, $orderBy);
        self::$TPL->assign('allVoucherGroups', $allVoucherGroups);

        return array(
            'type' => 'encoded_json',
            'content' => self::$TPL->display('setup/json_list_voucher_groups', true)
        );
    }
}